import api from './config'

// 创建 token
export interface CreateTokenRequest {
  uid?: string | null;
  limit?: number | null;
  timeOfHours?: number | null;
}

export interface CreateTokenResponse {
  token: string
  expireTime: number
}

export const createToken = async (data: CreateTokenRequest) => {
  return api.post('/api/docmee/token', data, {
    headers: {
      'Api-Key': import.meta.env.VITE_PPT_API_KEY
    }
  })
}

// 刷新token
export interface RefreshTokenRequest {
  token: string
}

export const refreshToken = async (data: RefreshTokenRequest) => {
  return api.post('/refreshApiToken', data, {
    headers: {
      'Api-Key': import.meta.env.VITE_PPT_API_KEY
    }
  })
}

// 创建任务
export interface CreateTaskRequest {
  type: 1 | 2 | 3 | 4 | 5 | 6 | 7
  content?: string
  file?: File[]
}

export interface CreateTaskResponse {
  id: string
}

export const createTask = async (data: CreateTaskRequest) => {
  const formData = new FormData()
  formData.append('type', data.type.toString())
  if (data.content) {
    formData.append('content', data.content)
  }
  if (data.file) {
    data.file.forEach(file => {
      formData.append('file', file)
    })
  }
  return api.post('/api/ppt/v2/createTask', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 生成内容
export interface GenerateContentRequest {
  id: string
  stream?: boolean
  length?: 'short' | 'medium' | 'long'
  audience?: string
  lang?: string
  prompt?: string
}

export interface GenerateContentResponse {
  text: string
  status: number
  result?: {
    level: number
    name: string
    children: any[]
  }
}

export const generateContent = async (data: GenerateContentRequest) => {
  return api.post('/api/ppt/v2/generateContent', data)
}

// 生成 PPT
export interface GeneratePptxRequest {
  id: string
  templateId?: string
  markdown?: string
}

export interface GeneratePptxResponse {
  pptInfo: {
    id: string
    subject: string
    coverUrl: string
    fileUrl: string
    templateId: string
    pptxProperty: string
    userId: string
    userName: string
    companyId: number
    updateTime: string | null
    createTime: string
  }
}

export const generatePptx = async (data: GeneratePptxRequest) => {
  return api.post('/api/ppt/v2/generatePptx', data)
}

// 获取选项
export interface OptionsResponse {
  lang: Array<{
    name: string
    value: string
  }>
  audience: Array<{
    name: string
    value: string
  }>
}

export const getOptions = async () => {
  return api.get<OptionsResponse>('/api/ppt/v2/options')
}
